/**
 * 
 */
package soa.security.web.export;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import v1.web.export.DefaultJsonFormatExport;
import v1.web.export.JsonExportCustomize;

/**
 * @author Cre.Gu
 *
 */
public class LdapJsonExportCustomize extends JsonExportCustomize {

	protected boolean appendFlat = false;

	public LdapJsonExportCustomize(boolean appendFlat) {
		super();
		this.appendFlat = appendFlat;
	}

	public LdapJsonExportCustomize() {
		super();
	}

	public LdapJsonExportCustomize(ObjectMapper mapper) {
		super(mapper);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * v1.web.export.JsonExportCustomize#customize(com.fasterxml.jackson.databind
	 * .JsonNode)
	 */
	@Override
	public void customize(JsonNode root) {
		System.out.println(root);
		if (root instanceof ArrayNode) {
			for (int i = 0; i < root.size(); i++) {
				cutomizeRecord((ObjectNode) root.get(i));
			}
		}
		if (root instanceof ObjectNode)
			cutomizeRecord((ObjectNode) root);
	}

	protected void cutomizeRecord(ObjectNode record) {
		if (appendFlat) {
			Map<String, String> map = new HashMap<String, String>();
			DefaultJsonFormatExport.toMap("", record, map);
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (record.has(entry.getKey()))
					;
				else {
					record.put(entry.getKey(), entry.getValue());
				}
			}
		}
	}
}
